package com.jy.appmarket.cards;

import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.internal.dismissanimation.BaseDismissAnimation;
import it.gmariotti.cardslib.library.internal.dismissanimation.SwipeDismissAnimation;

import java.text.SimpleDateFormat;
import java.util.Locale;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jy.app.market.idata.card.CardUpgrade;
import com.jy.app.market.idata.data.Upgrade;
import com.jy.appmarket.R;
import com.jy.appmarket.download.DownloadActivity;
import com.jy.appmarket.download.DownloadItem;
import com.jy.appmarket.download.DownloadService;
import com.jy.appmarket.download.DownloadUtils;
import com.jy.appmarket.statistics.ApkSource;
import com.jy.appmarket.utils.Prefs;
import com.jy.appmarket.view.UpgradeDetailView;

public class UpgradeUICard extends BaseUICard implements OnClickListener {

	private DownloadActivity mDLActivity;
	private Upgrade mUpgrade;
	private BaseDismissAnimation mDismissAnimation;
	private CardArrayAdapter mCardsAdapter;

	public UpgradeUICard(Context context, CardUpgrade upgrade) {
		super(context, R.layout.upgrade_card_layout);
		mDLActivity = (DownloadActivity) context;
		mUpgrade = upgrade.getUpgrade();

	}

	public void setParentAdapter(CardArrayAdapter adapter) {
		mCardsAdapter = adapter;
		if (adapter != null) {
			mDismissAnimation = new SwipeDismissAnimation(getContext()).setup(mCardsAdapter);
		}
	}

	public Upgrade getUpgrade() {
		return mUpgrade;
	}

	public void setUpgrade(Upgrade upgrade) {
		this.mUpgrade = upgrade;
	}

	@Override
	public int getType() {
		return 12;
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		TextView expandTitle = view.findViewById(R.id.content);
		expandTitle.setText(mUpgrade.getTitle());
		UpgradeDetailView expandView = view.findViewById(R.id.expand_text_view);
		expandView.setText(mUpgrade.getDetail());
		view.findViewById(R.id.upgrade).setOnClickListener(this);
		view.findViewById(R.id.ignore).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.upgrade:
			mDismissAnimation.animateDismiss(this);
			startUpgrade();
			saveShowCardCount();
			break;
		case R.id.ignore:
			mDismissAnimation.animateDismiss(this);
			saveShowCardCount();
			break;
		default:
			break;
		}
	}

	private void startUpgrade() {
		String dlId = DownloadUtils.generateDownloadId(getContext().getPackageName(),
				mUpgrade.getVersionCode());
		int status = mDLActivity.getDownloadStatus(dlId, mDLActivity.getPackageName(),
				mUpgrade.getVersionCode());
		switch (status) {
		case DownloadService.STATUS_CAN_INSTALL:
			DownloadItem downloadItem = mDLActivity.getDownloadById(dlId);
			mDLActivity.installApk(downloadItem);
			break;
		case DownloadService.STATUS_NO_STATUS:
			mDLActivity.addDownload(mUpgrade.getUrl(), mDLActivity.getString(R.string.app_name),
					mDLActivity.getPackageName(), null, mUpgrade.getVersionCode(),
					mUpgrade.getMd5(), mUpgrade.getIcon(), new ApkSource("upgrade").toSourceString());
			Toast.makeText(mDLActivity, R.string.upgrade_now_toast, Toast.LENGTH_SHORT).show();
			break;
		default:
			Toast.makeText(mDLActivity, R.string.upgrade_now_toast, Toast.LENGTH_SHORT).show();
			break;
		}

	}

	private void saveShowCardCount() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
		String currentDate = dateFormat.format(System.currentTimeMillis());
		Prefs.saveUpgradeCardDate(mDLActivity, currentDate);
	}
}
